package com.mypage.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.mypage.bean.CheckInBean;
import com.cloudcc.mobile.bull.GongniuCheckinActivity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/27 0027.
 */

public class NCheckDataAdapter extends BaseAdapter {

    public Activity mContext;
    public List<CheckInBean.Locations> mListData;

    public static boolean isSbClick = false;
    public static boolean isZwClick = false;
    public static boolean isXbClick = false;

    public NCheckDataAdapter(Activity context, List<CheckInBean.Locations> list) {
        this.mContext = context;
        this.mListData = list;
    }

    @Override
    public int getCount() {
        if (!mListData.isEmpty()) {
//            return mListData.size() >= 1 ? 1 : mListData.size();
            return mListData.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return mListData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        View convertView = View.inflate(mContext, R.layout.checkin_list_item, null);
        LinearLayout layoutList = (LinearLayout) convertView.findViewById(R.id.layoutList);

        /**
         * 顶端时间
         */
        TextView topTimeLine = (TextView) convertView.findViewById(R.id.topTimeLine);

        /**
         * 上班
         */
        TextView sbcontent = (TextView) convertView.findViewById(R.id.sbcontext);
        TextView sbtimes = (TextView) convertView.findViewById(R.id.sbtime);
        TextView sbworkName = (TextView) convertView.findViewById(R.id.sbworkName);
//        RelativeLayout sbLayout = (RelativeLayout) convertView.findViewById(R.id.sbLayout);
//        TextView sbData = (TextView) convertView.findViewById(R.id.sbData);

        /**
         * 中午
         */
        TextView zwcontent = (TextView) convertView.findViewById(R.id.zwcontext);
        TextView zwtimes = (TextView) convertView.findViewById(R.id.zwtime);
        TextView zwworkName = (TextView) convertView.findViewById(R.id.zwworkName);
//        TextView zwData = (TextView) convertView.findViewById(R.id.zwData);
        /**
         * 下班
         */
        TextView xbcontent = (TextView) convertView.findViewById(R.id.xbcontext);
        TextView xbtimes = (TextView) convertView.findViewById(R.id.xbtime);
        TextView xbworkName = (TextView) convertView.findViewById(R.id.xbworkName);
//        TextView xbData = (TextView) convertView.findViewById(R.id.xbData);


        if (null!=mListData.get(i).kqrq){
            topTimeLine.setText(mListData.get(i).kqrq+"  "+setDataDay(mListData.get(i).kqrq));
        }


        if (null == mListData.get(i).bgsj) {//上班时间为空
//            sbLayout.setVisibility(View.GONE);
            setSb(true);
        } else {
            setSb(false);
//            sbLayout.setVisibility(View.VISIBLE);
            sbcontent.setText(mListData.get(i).qddz);
            sbtimes.setText(mListData.get(i).bgsj.substring(11, mListData.get(i).bgsj.length()));
            sbworkName.setText("上班");
//            sbData.setText(setDataDay(mListData.get(i).bgsj));
        }

        if (null == mListData.get(i).zwbgsj) {//中午时间为空
//            zwLayout.setVisibility(View.GONE);
            setZw(true);
        } else {
            setZw(false);
//            zwLayout.setVisibility(View.VISIBLE);
            zwcontent.setText(mListData.get(i).zwqddz);
            zwtimes.setText(mListData.get(i).zwbgsj.substring(11, mListData.get(i).zwbgsj.length()));
            zwworkName.setText("中午");
//            zwData.setText(setDataDay(mListData.get(i).zwbgsj));
        }

        if (null == mListData.get(i).xbbgsj) {//下班时间为空
//            xbLayout.setVisibility(View.GONE);
            setXb(true);
        } else {
            setXb(false);
//            xbLayout.setVisibility(View.VISIBLE);
            xbcontent.setText(mListData.get(i).xbqtdz);
            xbtimes.setText(mListData.get(i).xbbgsj.substring(11, mListData.get(i).xbbgsj.length()));
            xbworkName.setText("下班");
//            xbData.setText(setDataDay(mListData.get(i).xbbgsj));
        }

        if (getDatea().equals(mListData.get(i).kqrq)) {
            GongniuCheckinActivity.instancesa.setButtonLineBtn();
            return convertView;
        } else {
            layoutList.removeAllViews();
            layoutList.setVisibility(View.GONE);
            setSb(true);
            setZw(true);
            setXb(true);
            GongniuCheckinActivity.instancesa.setButtonLineBtn();
            return convertView;
        }

    }

    public String setDataDay(String data) {
        final String dayNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
                "星期六"};
        String s = data;
//        SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        try {
            date = sdfInput.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek < 0) dayOfWeek = 0;
        System.out.println(dayNames[dayOfWeek]);
        return dayNames[dayOfWeek];
    }

    public static void setSb(boolean bool) {
        isSbClick = bool;
    }

    public static boolean getSb() {
        return isSbClick;
    }

    public static void setZw(boolean bool) {
        isZwClick = bool;
    }

    public static boolean getZw() {
        return isZwClick;
    }

    public static void setXb(boolean bool) {
        isXbClick = bool;
    }

    public static boolean getXb() {
        return isXbClick;
    }

    public String getDatea() {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(d);
//        return "('" + sdf.format(d) + "','yyyy-MM-dd')";
    }
}
